<?

Module::Init('system.structure');
Module::Init('system.fields');


$filter = isset($params['filter']) ? $params['filter'] : false;
$sort = isset($params['sort']) ? $params['sort'] : false;

$listObj = BDSSection::GetList($filter, $sort);
$sections = array();

while ($section = $listObj->Fetch()) {
    $section['fields'] = Fields::GetFields($section['id'], $section['block_id'], 'S');
    $section['element_count'] = BDSElement::GetCount(array(array('field' => 'section_id', 'value' => $section['id'])));
    $section['section_count'] = BDSSection::GetCount(array(array('field' => 'section_id', 'value' => $section['id'])));
    $sections[] = $section;
}
$result = Fields::__tree($sections, 'section_id');